package com.beagle.njddapp.ui.presenter;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.ZdBean;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.bean.owner.DirverBean;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.BaseStringResponseBean;
import com.beagle.njddapp.net.BaseZdHttpListResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.AddDirverView;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class AddDirverPresenter extends BasePresenter<AddDirverView> {
    private NjOwnerApiService njOwnerApiService;
    private ApiService apiService;

    public AddDirverPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        njOwnerApiService = retrofit.create(NjOwnerApiService.class);
        apiService = retrofit.create(ApiService.class);

    }




    public void addFindDriverOrder(DirverBean dirverBean) {
        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new HashMap<>();
        paramsMap.put("type", "workerRequest");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        apiService.requestFormToken(body).compose(RxSchedulers.compose()).safeSubscribe(new Observer<BaseStringResponseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseStringResponseBean baseStringResponseBean) {
                Gson gson = new Gson();
                HashMap<String, String> paramsMap = new LinkedHashMap<>();
                paramsMap.put("beginDate", dirverBean.getBeginDate());
                paramsMap.put("endDate", dirverBean.getEndDate());
                paramsMap.put("meshId", dirverBean.getMeshId());
                paramsMap.put("meshNum", dirverBean.getMeshNum());
                paramsMap.put("workType", dirverBean.getWorkType());
                paramsMap.put("workAddress", dirverBean.getWorkAddress());
                paramsMap.put("plantLng", dirverBean.getPlantLng());
                paramsMap.put("plantLat", dirverBean.getPlantLat());
                paramsMap.put("workPrice", dirverBean.getWorkPrice());
                paramsMap.put("formToken", baseStringResponseBean.getResult());
                String strEntity = gson.toJson(paramsMap);
                RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
                paramsMap.clear();
                njOwnerApiService.addFindDriverOrder(body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
                    @Override
                    public void success(BaseOilHttpResponse oilHttpResponse) {
                        getView().addSuccess();

                    }
                    @Override
                    public void error(int code, String message) {
                        LogUtils.d("error");
                        getView().onError(message);

                    }
                });
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("服务异常");
            }

            @Override
            public void onComplete() {

            }
        });

    }

    public void getDeviceListRun(int size,int page){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("state","");
        paramsMap.put("latitude","");
        paramsMap.put("longitude","");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        njOwnerApiService.getDeviceList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<NjBean>>() {


            @Override
            public void success(BaseOilHttpListResponse<NjBean> baseOilHttpListResponse) {
                if(baseOilHttpListResponse!=null&&baseOilHttpListResponse.results!=null){
                    getView().showRunNj(baseOilHttpListResponse.results);
                }

            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);

            }
        });
    }
    public void updateFindDriverOrder(DIrverOrderBean dIrverOrderBean){
        Gson gson=new Gson();
        String strEntity = gson.toJson(dIrverOrderBean);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        njOwnerApiService.updateFindDriverOrder(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse baseOilHttpResponse) {
                getView().updateSuccess();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

    public void queryDictList(String type, String parentId) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("type", type);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        njOwnerApiService.queryDictList(body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseZdHttpListResponse<ZdBean>>() {


            @Override
            public void success(BaseZdHttpListResponse<ZdBean> zdHttpListResponse) {
                LogUtils.d("success");
                if (zdHttpListResponse.items!=null){
                    getView().showZd(zdHttpListResponse.items);
                }else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                LogUtils.d("error");
                getView().onError(message);
            }
        });

    }
}
